﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.Core.Entities;
using System.Text;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace AMS.Web.BaseData
{
    public partial class Ae : Page
    {

        readonly BaseDataManager m_Help = new BaseDataManager();
        private string functionString = "";
        PermissionManager m_PermissionManager = new PermissionManager();

        private string MenuId
        {
            get
            {

                if (ViewState["MenuId"] != null)
                    return ViewState["MenuId"].ToString();
                else
                    return "";
            }
            set { ViewState["MenuId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                if (Request["MenuId"] != null)
                    MenuId = Request["MenuId"].Trim();
             
              if (!m_PermissionManager.CheckPageIsCanDelete(this))
                  btnDelete.Enabled = false;
              ShowTree();
            }
       
              if (!m_PermissionManager.CheckPageIsCanAdd(this))
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "abc", "<script>CheckPermission('add')</script>");
              if (!m_PermissionManager.CheckPageIsCanEdit(this))
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "abcd", "<script>CheckPermission('edit')</script>");
            
            
        }

        private void ShowTree()
        {
            TreeView_Area.Nodes.Clear();

            TreeNode node_0 = new TreeNode();
            node_0.ImageUrl = "../../Images/calendar.gif";
            node_0.Text = "区域管理" + "<a herf='#'></a>";
            node_0.Value = "0";
            node_0.Text += "&nbsp;&nbsp;<a target='_self' href='AreaEdit.aspx?MenuId=" + MenuId + "&operater=Insert&id=" +
                publ.GetUrlToSend(node_0.Value.Trim()) + "'><img style='width:16px;height:16px' src='../Images/msnadd.gif' class='add' alt='新增区域'/></a>";
            node_0.SelectAction = TreeNodeSelectAction.Expand;
            node_0.Expanded = true;
            TreeView_Area.Nodes.Add(node_0);
            ViewState["ds"] =
            m_Help.GetDataSetBySql("SELECT m.* FROM AmsCore_Area m   WHERE 1 = 1  and  m.ParentId is null order by Code");
            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);

            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                Node.Text = Row["Name"].ToString().Trim() + "<a herf='#'></a>"; ;
                Node.Value = Row["id"].ToString().Trim();
                Node.ImageUrl = "../../Images/chart_bar.gif";
                if (Row["Code"].ToString().Length != 0)
                {
                    Node.Text += "<font style='font-size: 11px' color='#808080'>[" + Row["Code"].ToString() + "]</font> ";
                }
                Node.Text += "&nbsp;&nbsp;<a target='_self' href='AreaEdit.aspx?MenuId=" + MenuId + "&operater=Insert&id=" +
                    publ.GetUrlToSend(Node.Value.Trim()) + "'><img style='width:16px;height:16px' src='../Images/msnadd.gif' class='add' alt='新增子区域'/></a>";
                Node.Text += "&nbsp;&nbsp;<a target='_self' href='AreaEdit.aspx?MenuId=" + MenuId + "&operater=Update&id=" +
                    publ.GetUrlToSend(Node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='../Images/edit.gif' class='edit' alt='编辑'/></a>";

                node_0.ChildNodes.Add(Node);
                Node.Expanded = true;
                AddTree(Node.Value, Node);
            }

        }

        public void AddTree(String parentId, TreeNode pNode)
        {
            IList objParentList = m_Help.GetObjectList(new Area().GetType().Name, "Id", parentId.Trim());
            if (objParentList != null && objParentList.Count > 0)
            {
                var objParentArea = (Area)objParentList[0];
                IList objList = m_Help.Session.CreateQuery("FROM Area where Parent='" +
                                     objParentArea.Id.ToString().Trim() + "' order by Code").List();
 
                if (objList != null && objList.Count > 0)
                {
                    foreach (Area currentArea in objList)
                    {
                        var node = new TreeNode
                                       {
                                           Value = currentArea.Id.ToString().Trim(),
                                           Text = currentArea.Name.Trim() + "<a herf='#'></a>"
                                       };


                        if (currentArea.Code.Trim().Length != 0)
                        {
                            node.Text += "<font style='font-size: 11px' color='#808080'>[" + currentArea.Code.Trim() + "]</font> ";
                        }
                        node.Text += "&nbsp;&nbsp;<a target='_self' href='AreaEdit.aspx?MenuId=" + MenuId + "&operater=Insert&id=" +
                        publ.GetUrlToSend(node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='../Images/msnadd.gif' class='add' alt='新增子区域'/></a>";
                        node.Text += "&nbsp;&nbsp;<a target='_self' href='AreaEdit.aspx?MenuId=" + MenuId + "&operater=Update&id=" +
                        publ.GetUrlToSend(node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='../Images/edit.gif' class='edit' alt='编辑'/></a>";

                        pNode.ChildNodes.Add(node);
                        node.Expanded = true;
                        node.ImageUrl = "../../Images/notes.gif";


                        AddTree(currentArea.Id.ToString().Trim(), node);    //再次递归
                    }
                }
            }

        }

        /// <summary>
        /// 得到所选择的节点,调用自身函数循环遍历TreeView
        /// </summary>
        /// <param name="node"></param>
        /// <returns>返回FunctionString字符串</returns>
        public string GetTreeCheck(TreeNode node)
        {

            foreach (TreeNode treenode in node.ChildNodes)
            {
                if (treenode.Checked)
                {
                    string va = treenode.Value;
                    functionString += ", " + va + "";
                }

                GetTreeCheck(treenode);

            }
            return functionString;
        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strAreaCodeAll = GetTreeCheck(TreeView_Area.Nodes[0]);
            bool access = false;
            if (string.IsNullOrEmpty(strAreaCodeAll))
            {

                ClientScript.RegisterStartupScript(ClientScript.GetType(), "005", "<script>alert('请选择要删除的区域!')</script>");
                return;
            }
            StringCollection scMsg;
            string[] ss = strAreaCodeAll.Split(',');
            for (int i = 0; i < ss.Length; i++)
            {

                if (!string.IsNullOrEmpty(ss[i]))
                {
                    if (m_Help.SelectAreaById(ss[i].Trim(),out scMsg))
                    {

                        if (scMsg.Count > 0)
                        {
                            var sb = new StringBuilder();
                            for (int j = 0; j < scMsg.Count; j++)
                                sb.Append(scMsg[j] + "\r\n");
                            Alert.MessageBox(sb.ToString());
                        }
                     
                        return;
                    }
                    access = m_Help.DeleteObjById(new Area(), ss[i].Trim());

                }

            }
            if (access)
            {

                ClientScript.RegisterStartupScript(ClientScript.GetType(), "007", "<script>alert('删除成功!')</script>");
            }

            ShowTree();
        }
    }
}
